package com.athena.controller 
{
	import com.adobe.serialization.json.JSON;
	import com.athena.utils.Delegate;
	import com.greensock.loading.SWFLoader;
	import com.junkbyte.console.Cc;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	/**
	 * ...
	 * @author ...
	 */
	public class Rooms
	{
		public static var ADD_ROOM : String = "add_room";
		
		private var _id : int;
		private var arrayRoom : Array = [];
		public function Rooms(_dash : MovieClip, str : String) 
		{
			var arr : Array = JSON.decode(str);
			var i : int = 0;
			for each(var r : Object in arr) {
				var cls : Class = (Main.getInstance().loaderFile.queue.getLoader(Main.getInstance().config.dashboard.name) as SWFLoader).getClass("com.athena.ui.ButtonRoom") as Class;
				var room : MovieClip = MovieClip(new cls());
				room.width = 90;
				room.x = 15 + 90 * i; room.y = 9;
				room.gotoAndStop(1);
				_dash.lobby_mc.addChild(room);
				arrayRoom.push(room);
				room.addEventListener(MouseEvent.ROLL_OVER, Delegate.create(onRollOverHandler, r.Id));
				room.addEventListener(MouseEvent.ROLL_OUT, Delegate.create(onRollOutHandler, r.Id));
				room.addEventListener(MouseEvent.MOUSE_DOWN, onPressHandler);
				
				room.addEventListener(MouseEvent.CLICK, Delegate.create(clickRoom, r.Id));
				(room.getChildAt(1) as TextField).text = r.Name;
				i++;
			}
		}
		
		private function onRollOverHandler(e : MouseEvent, _ids : int) : void {
			Mouse.cursor = MouseCursor.BUTTON;
			if(_id != _ids)
				(e.currentTarget as MovieClip).gotoAndStop(2);
			
		}
		private function onRollOutHandler(e : MouseEvent, _ids : int) : void {
			Mouse.cursor = MouseCursor.AUTO;
			if(_id != _ids)
				(e.currentTarget as MovieClip).gotoAndStop(1);
		}
		private function onPressHandler(e : MouseEvent) : void {
			//(e.currentTarget as MovieClip).gotoAndStop(3);
		}
		private function clickRoom(e : MouseEvent, _ids : int) : void {
			if (_id != _ids) {
				_id = _ids;
				for each(var btn : MovieClip in  arrayRoom) {
					btn.gotoAndStop(1);
				}
				(e.currentTarget as MovieClip).gotoAndStop(3);
			}
			var obj : Object = { };
			obj.evt = "selectR";
			obj.id = id;
			Main.getInstance().socket.sendService(JSON.encode(obj));
			obj = null;	
		}
		
		public function get id():int 
		{
			return _id;
		}
	}

}